package main

import (
	"context"
	"fmt"
	p1 "gRPCStudy/p1"
	"google.golang.org/grpc"
	"net"
)

type Server struct {
	p1.GreeterClient
}

func (s *Server) SayHello(ctx context.Context, in *p1.HelloRequest, opts ...grpc.CallOption) (*p1.HelloReply, error) {
	return &p1.HelloReply{Message: "conect server successful"}, nil
}

func main() {
	//基于tcp实现
	listen, err := net.Listen("tcp", ":1099")
	if err != nil {
		fmt.Println("failed to listen", err)
	}
	//创建grpc服务
	server := grpc.NewServer()

	//*************** 关键  **********
	//注册自定义方法
	p1.RegisterGreeterServer(server, &Server{})
	// *********************************

	//启动服务
	err = server.Serve(listen)
	if err != nil {
		fmt.Println("failed to serve", err)
		return
	}
}
